package com.feihong.txgw.adapter.Holder;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.HomeGridViewAdapter;
import com.feihong.txgw.json.HomeJSON;
import com.feihong.txgw.model.bean.kaika.KaikaNewPackageEntity;
import com.feihong.txgw.model.PRODUCT_TYPE;
import com.feihong.txgw.model.bean.home.ProductBaseModel;
import com.feihong.txgw.ui.activity.HomeActivity;
import com.feihong.txgw.ui.activity.kaika.KaiKaStep2Activity;
import com.feihong.txgw.ui.activity.kaika.KaikaNewPackageActivity;
import com.feihong.txgw.ui.activity.kaika.KaikaNoCardOperateActivity;
import com.feihong.txgw.ui.activity.product.ProductInfoActivity;
import com.feihong.txgw.ui.activity.product.ProductListActivity;
import com.feihong.txgw.ui.activity.user.LoginActivity;
import com.feihong.txgw.utils.UserLoginInfo;
import com.feihong.txgw.utils.glidehttps.GlideOptionsSetting;
import com.feihong.txgw.utils.kaika.KaikaIntentType;
import com.feihong.txgw.view.NoScrollGridView;

import cn.pedant.SweetAlert.SweetAlertDialog;

import static com.feihong.txgw.ui.fragment.home.DiscoverFragment.INTENT_PRODUCTID;

/**
 * Created by Sabirjan on 2017/6/23.
 * TXGW2.
 * 飞宏电子
 */

public class HomeGridViewHolder extends BaseViewHolder {
    private TextView gridTitle;
    private TextView gridTitleDes;
    private LinearLayout homeGridFristLine;
    private ImageView homeGridImageLarge;
    private TextView homeGridTitleLarge;
    private TextView homeGridDesLarge;
    private ImageView homeGridImageSmall;
    private TextView homeGridTitleSmall;
    private TextView homeGridDesSmall;
    private NoScrollGridView homeHolderGridview;
    private LinearLayout homeGridMoreview;

    private boolean isBind = false;

    public void init(View view) {
        gridTitle = (TextView) view.findViewById(R.id.grid_title);
        gridTitleDes = (TextView) view.findViewById(R.id.grid_title_des);
        homeGridFristLine = (LinearLayout) view.findViewById(R.id.home_grid_fristLine);
        homeGridImageLarge = (ImageView) view.findViewById(R.id.home_grid_image_large);
        homeGridTitleLarge = (TextView) view.findViewById(R.id.home_grid_title_large);
        homeGridDesLarge = (TextView) view.findViewById(R.id.home_grid_des_large);
        homeGridImageSmall = (ImageView) view.findViewById(R.id.home_grid_image_small);
        homeGridTitleSmall = (TextView) view.findViewById(R.id.home_grid_title_small);
        homeGridDesSmall = (TextView) view.findViewById(R.id.home_grid_des_small);
        homeHolderGridview = (NoScrollGridView) view.findViewById(R.id.home_holder_gridview);
        homeGridMoreview = (LinearLayout) view.findViewById(R.id.home_grid_moreview);

        view.findViewById(R.id.layout_home_smallItem).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (smallModel != null && smallModel.getId() != null) {

                    if (PRODUCT_TYPE.TAG_PHONE.equals(smallModel.getType())) {

                        Intent intent = new Intent();
                        intent.setClass(mContext, ProductInfoActivity.class);
                        intent.putExtra(INTENT_PRODUCTID, smallModel.getId());
                        intent.putExtra(ProductInfoActivity.INTENT_NAME, smallModel.getName());
                        String uriString = intent.toUri(Intent.URI_INTENT_SCHEME);
                        Log.i("页面", "onClick: " + uriString);
                        mContext.startActivity(intent);
                    }
                }
            }
        });

        view.findViewById(R.id.layout_home_largeItem).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (largeModel != null && largeModel.getId() != null) {
                        if (UserLoginInfo.isUserLogin()) {
                            Intent intent = new Intent();
                            intent.setClass(mContext, ProductInfoActivity.class);
                            intent.putExtra(INTENT_PRODUCTID, largeModel.getId());
                            intent.putExtra(ProductInfoActivity.INTENT_NAME, largeModel.getName());
                            mContext.startActivity(intent);
                    }
                }
            }
        });
    }

    private HomeGridViewAdapter gridViewAdapter;
    private Context mContext;
    private HomeJSON.HomeDataBean dataBean;
    private int viewType = 0;
    private ProductBaseModel largeModel = null;
    private ProductBaseModel smallModel = null;

    private static long lastClickTime;
    private static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 1000 * 2) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
    public HomeGridViewHolder(Context context, int type, View itemView) {
        super(itemView);
        this.mContext = context;
        this.viewType = type;
        init(itemView);
        homeHolderGridview.setFocusable(false);
        homeHolderGridview.setNumColumns(2);
        if (type == PRODUCT_TYPE.TYPE_PHONE_NUM || type == PRODUCT_TYPE.TYPE_PHONE_TARIFF) {
            homeGridFristLine.setVisibility(View.GONE);
        }
        homeHolderGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position < dataBean.getItems().size()) {
                    if (PRODUCT_TYPE.TAG_PHONE.equals(dataBean.getItems().get(position).getType())) {
                        Intent intent = new Intent();
                        intent.setClass(mContext, ProductInfoActivity.class);
                        intent.putExtra(INTENT_PRODUCTID, dataBean.getItems().get(position).getId());
                        intent.putExtra(ProductInfoActivity.INTENT_NAME, dataBean.getItems().get(position).getName());
                        mContext.startActivity(intent);
                    } else if (PRODUCT_TYPE.TAG_PHONE_TARIFF.equals(dataBean.getList_type())) {
                        // 单个套餐点击跳转
                        if (UserLoginInfo.isUserLogin()) {
                            Intent intent = new Intent();
                            intent.setClass(mContext, KaikaNoCardOperateActivity.class);
                            Bundle bundle = new Bundle();
                            KaikaNewPackageEntity packageEntity = new KaikaNewPackageEntity();
                            packageEntity.setSpec_name(dataBean.getItems().get(position).getName());
                            packageEntity.setSpec_id(dataBean.getItems().get(position).getId());
                            packageEntity.setType(PRODUCT_TYPE.TAG_PHONE_TARIFF);
                            bundle.putParcelable(KaikaIntentType.HOME_INTENT_PACAKGE_INFO_KEY, packageEntity);
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
                        } else {
                            if(isFastDoubleClick()) {
                                return;
                            }
                            HomeActivity homeActivity = (HomeActivity) mContext;
                            homeActivity.showConfirmDialog("未登录", "您还未登录，请先登录！", "登录", "取消",
                                    new SweetAlertDialog.OnSweetClickListener() {
                                        @Override
                                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                                            sweetAlertDialog.dismiss();
                                            Intent loginIntent = new Intent();
                                            loginIntent.setClass(mContext, LoginActivity.class);
                                            mContext.startActivity(loginIntent);
                                        }
                                    });
                        }
                    }else if(PRODUCT_TYPE.TAG_PHONE_NUM.equals(dataBean.getList_type())){
                        // 单个号码点击跳转
                        if (UserLoginInfo.isUserLogin()) {
                            Intent intent = new Intent();
                            intent.setClass(mContext, KaikaNoCardOperateActivity.class);
                            Bundle bundle = new Bundle();
                            KaikaNewPackageEntity packageEntity = new KaikaNewPackageEntity();
                            packageEntity.setNumber(dataBean.getItems().get(position).getNumber());
                            packageEntity.setNumberId(dataBean.getItems().get(position).getId());
                            packageEntity.setType(PRODUCT_TYPE.TAG_PHONE_NUM);
                            bundle.putParcelable(KaikaIntentType.HOME_INTENT_PACAKGE_INFO_KEY, packageEntity);
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
                        } else {
                            if(isFastDoubleClick()) {
                                return;
                            }
                            HomeActivity homeActivity = (HomeActivity) mContext;
                            homeActivity.showConfirmDialog("未登录", "您还未登录，请先登录！", "登录", "取消",
                                    new SweetAlertDialog.OnSweetClickListener() {
                                        @Override
                                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                                            sweetAlertDialog.dismiss();
                                            Intent loginIntent = new Intent();
                                            loginIntent.setClass(mContext, LoginActivity.class);
                                            mContext.startActivity(loginIntent);
                                        }
                                    });
                        }
                    }
                }
            }
        });
        homeGridMoreview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (dataBean != null && PRODUCT_TYPE.TAG_PHONE.equals(dataBean.getList_type())) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, ProductListActivity.class);
                    mContext.startActivity(intent);
                }
                if (PRODUCT_TYPE.TAG_PHONE_TARIFF.equals(dataBean.getList_type())) {
                    if(UserLoginInfo.isUserLogin()){
                        Intent intent = new Intent(mContext, KaikaNewPackageActivity.class);
                        intent.putExtra(KaikaIntentType.PACKAGE_INTENT_TYPE_KEY, KaikaIntentType.INTENT_FROM_HOME_PAGE);
                        mContext.startActivity(intent);
                    }else {
                        HomeActivity homeActivity = (HomeActivity) mContext;
                        homeActivity.showConfirmDialog("未登录", "您还未登录，请先登录！", "登录", "取消",
                                new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        sweetAlertDialog.dismiss();
                                        Intent loginIntent = new Intent();
                                        loginIntent.setClass(mContext, LoginActivity.class);
                                        mContext.startActivity(loginIntent);
                                    }
                                });
                    }
                }else if(PRODUCT_TYPE.TAG_PHONE_NUM.equals(dataBean.getList_type())){
                    if(UserLoginInfo.isUserLogin()){
                        Intent intent = new Intent(mContext, KaiKaStep2Activity.class);
                        intent.putExtra(KaikaIntentType.PHONE_NUMBER_INTENT_TYPE_KEY, KaikaIntentType.INTENT_FROM_HOME_PAGE);
                        mContext.startActivity(intent);
                    }else {
                        HomeActivity homeActivity = (HomeActivity) mContext;
                        homeActivity.showConfirmDialog("未登录", "您还未登录，请先登录！", "登录", "取消",
                                new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        sweetAlertDialog.dismiss();
                                        Intent loginIntent = new Intent();
                                        loginIntent.setClass(mContext, LoginActivity.class);
                                        mContext.startActivity(loginIntent);
                                    }
                                });
                    }
                }

            }
        });

    }

    /**
     * 判断首页列表第一个图片是否宽形式
     *
     * @return
     */
    private boolean checkHaveLargeModel() {

        if (this.dataBean.getItems().size() < 2) {
            return false;
        }

        if (this.dataBean.getItems().get(0) != null && this.dataBean.getItems().get(0).getShowtype() != null) {
            return "large".contentEquals(this.dataBean.getItems().get(0).getShowtype().toLowerCase());
        }
        return false;
    }

    public void bindData(HomeJSON.HomeDataBean dataBean) {

        if (isBind == true) {
            return;
        }


        this.dataBean = dataBean;

        if (this.dataBean != null) {
            gridTitle.setText(this.dataBean.getList_name());
            gridTitleDes.setText(this.dataBean.getList_des());
            if (checkHaveLargeModel()) {
                if (this.dataBean.getItems().size() > 1) {
                    homeGridFristLine.setVisibility(View.VISIBLE);
                    largeModel = this.dataBean.getItems().get(0);
                    smallModel = this.dataBean.getItems().get(1);
                    Glide.with(mContext).load(largeModel.getPic()).apply(GlideOptionsSetting.getDefaultOptions(R.mipmap.ic_launcher,R.mipmap.ic_launcher)).into(homeGridImageLarge);
                    Glide.with(mContext).load(smallModel.getPic()).apply(GlideOptionsSetting.getDefaultOptions(R.mipmap.ic_launcher,R.mipmap.ic_launcher)).into(homeGridImageSmall);
                    homeGridTitleLarge.setText(largeModel.getName());
                    homeGridTitleSmall.setText(smallModel.getName());
                    homeGridDesLarge.setText(largeModel.getDes());
                    homeGridDesSmall.setText(smallModel.getDes());
                    this.dataBean.getItems().remove(0);//删除第一个元素，删除后以前的第二个元素变成第一个了
                    this.dataBean.getItems().remove(0);//删除第一个元素
                    gridViewAdapter = new HomeGridViewAdapter(mContext, viewType, this.dataBean.getItems());
                    homeHolderGridview.setAdapter(gridViewAdapter);
                } else {
                    //绑定普通的
                    homeGridFristLine.setVisibility(View.GONE);
                    gridViewAdapter = new HomeGridViewAdapter(mContext, viewType, this.dataBean.getItems());
                    homeHolderGridview.setAdapter(gridViewAdapter);
                }
            } else {
                //绑定普通的
                homeGridFristLine.setVisibility(View.GONE);
                gridViewAdapter = new HomeGridViewAdapter(mContext, viewType, this.dataBean.getItems());
                homeHolderGridview.setAdapter(gridViewAdapter);
            }
            isBind = true;
        }
    }
}
